package cj.hrm.service.impl;

import cj.hrm.dao.EmployeeDao;
import cj.hrm.dto.excel.EmployeeToExcel;
import cj.hrm.entity.*;
import cj.hrm.entity.request.EmployeeToExcelRequest;
import cj.hrm.service.EmployeeExcelService;
import cj.hrm.service.cache.InitHrmCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Provider;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author: <a href="mailto:get_xiaobo@163.com">Joshxu</a>
 * @description:
 * @create: 2021/5/8 10:51 下午
 **/
@Service
public class EmployeeExcelServiceImpl implements EmployeeExcelService {

    private static final Logger log = LoggerFactory.getLogger(EmployeeExcelServiceImpl.class);

    private final InitHrmCache initHrmCache;
    private final EmployeeDao employeeDao;

    @Autowired
    public EmployeeExcelServiceImpl(Provider<InitHrmCache> initHrmCache, EmployeeDao employeeDao) {
        this.initHrmCache = initHrmCache.get();
        this.employeeDao = employeeDao;
    }

    /**
     * @param list
     * @return
     */
    @Override
    @Transactional
    public boolean employeeExcelDataToDB(List<EmployeeToExcel> list) {
        Map<String, Integer> sexMap = initHrmCache.getSexList().stream().collect(Collectors.toMap(Sex::getName, Sex::getId));
        Map<String, Integer> deptMap = initHrmCache.getDeptList().stream().collect(Collectors.toMap(Dept::getName, Dept::getId));
        Map<String, Integer> educationMap = initHrmCache.getEducationList().stream().collect(Collectors.toMap(Education::getName, Education::getId));
        Map<String, Integer> jobMap = initHrmCache.getJobList().stream().collect(Collectors.toMap(Job::getName, Job::getId));
        Map<String, Integer> userMap = initHrmCache.getUserList().stream().collect(Collectors.toMap(User::getLoginname, User::getId));

        List<EmployeeToExcelRequest> employeeList = list.stream().map(employee -> {
            String sex = Optional.ofNullable(employee.getSex()).orElse("");
            String dept = Optional.ofNullable(employee.getDepartment()).orElse("");
            String education = Optional.ofNullable(employee.getEducation()).orElse("");
            String position = Optional.ofNullable(employee.getPosition()).orElse("");
            String associatedUser = Optional.ofNullable(employee.getAssociatedUser()).orElse("");
            return EmployeeToExcelRequest.builder()
                    .sex(sexMap.get(sex))//性别
                    .department(deptMap.get(dept))//部门
                    .education(educationMap.get(education))//学历
                    .jobId(jobMap.get(position))//职位
                    .name(employee.getName())//名字
                    .email(employee.getEmail())
                    .idCard(employee.getIdCard())
                    .address(employee.getAddress())
                    .number(employee.getNumber())//编码
                    .associatedUser(userMap.get(associatedUser))//推荐人
                    .build();
        }).collect(Collectors.toList());

        int flag = this.employeeDao.batchExcelInsert(employeeList);

        return flag != 0;
    }
}
